import java.util.Arrays;

/**
 * @author wangj881
 * @date 2025-01-03 17:01
 */
public class L238_3 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};
        int[] ints = new Solution().productExceptSelf(nums);
        System.out.println(Arrays.toString(ints));
    }

    static class Solution {
        public int[] productExceptSelf(int[] nums) {
            int length = nums.length;
            int[] res = new int[length];
            int[] pre = new int[length];
            int[] suf = new int[length];
            pre[0] = 1;
            for (int i = 1; i < nums.length; i++) {
                pre[i] = pre[i - 1] * nums[i - 1];
            }
            suf[nums.length - 1] = 1;
            for (int j = nums.length - 2; j >= 0; j--) {
                suf[j] = suf[j + 1] * nums[j + 1];
            }
            for (int i = 0; i < length; i++) {
                res[i] = pre[i] * suf[i];
            }
            return res;
        }
    }
}
